package com.atguigu.b_fileinputstream;

import java.io.FileInputStream;
import java.io.IOException;

/*
    一次一个字节数组的逻辑读文件
 */
public class Demo2 {
    public static void main(String[] args) throws IOException {
        //创建流对象
        FileInputStream fis = new FileInputStream("T_Day22\\test\\Demo1.java");

        //定义数组 -> 用来存每次读到的字节数据 -> byte[] bys
        byte[] bys = new byte[1024];

        //定义统计变量 len -> 统计每次读到的字节个数
        int len;

        //循环读写
        while((len = fis.read(bys)) != -1){
            //能进来说明读到了 -> 转换成字符串 [读了多少转多少]
            String line = new String(bys, 0, len);
            System.out.print(line);
        }

        //关流
        fis.close();
    }

    private static void extracted(FileInputStream fis) throws IOException {
        //一次一个字节数组的逻辑读文件

        //创建数组来存储每次读到的字节数据
        byte[] bys = new byte[5];
        //返回值代表读到的字节个数
        int len = fis.read(bys);

        //byte[] --> 字符串 : 读了多少转多少
        String line = new String(bys,0,len);
        System.out.println("len = " + len);//5
        System.out.print(line);//Hello
        System.out.println();
        System.out.println("-----------------------");
        //第二次读
        len = fis.read(bys);
        //byte[] --> 字符串
        line = new String(bys,0,len);
        System.out.println("len = " + len);//5
        System.out.print(line);//\r\nWor
        System.out.println();

        System.out.println("-----------------------");
        //第三次读
        len = fis.read(bys);
        //byte[] --> 字符串
        line = new String(bys,0,len);
        System.out.println("len = " + len);//2
        System.out.print(line);//ld
        System.out.println();
        System.out.println("-----------------------");

        //读到文件末尾了 继续读 :
        System.out.println(fis.read(bys));
        System.out.println(fis.read(bys));
        System.out.println(fis.read(bys));
        System.out.println(fis.read(bys));
        System.out.println(fis.read(bys));
    }
}
